package com.zht.handler;

import com.zht.common.Result;
import com.zht.enmus.ErrorCode;
import com.zht.exception.ServerException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(ServerException.class)
    public Result<?> handleServiceException(ServerException e) {
        return Result.error(e.getErrorCode());
    }

    @ExceptionHandler(Exception.class)
    public Result<?> handleGlobalException(Exception e) {
        e.printStackTrace(); // 生产环境中应记录到日志文件
        return Result.error(ErrorCode.INTERNAL_SERVER_ERROR);
    }
}